{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [2 4 6]\n",
      " [3 6 9]]\n",
      "该矩阵的维数: 2\n",
      "此矩阵的形状: (3, 3)\n",
      "矩阵一共有多少元素: 9\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "array = np.array([[1,2,3],[2,4,6],[3,6,9]]) #将列表转化成矩阵\n",
    "print(array)\n",
    "print('该矩阵的维数:',array.ndim)\n",
    "print('此矩阵的形状:',array.shape)\n",
    "print('矩阵一共有多少元素:',array.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[ 3.14   6.25  32.222]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3],dtype=np.int32)#定义数据类型\n",
    "b = np.array([3.14,6.25,32.222],dtype=np.float32)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "[[1 1 1]\n",
      " [1 1 1]]\n",
      "[ 0  2  4  6  8 10]\n",
      "[[ 0  2  4]\n",
      " [ 6  8 10]]\n",
      "[ 1.    3.25  5.5   7.75 10.  ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.zeros((3,4))\n",
    "b = np.ones((2,3),dtype = np.int32)\n",
    "print(a)\n",
    "print(b)\n",
    "c = np.arange(0,12,2) #生成步长为2的从0到12的数字[0,10）\n",
    "print(c)\n",
    "c = c.reshape((2,3))#重新定义形状\n",
    "print(c)\n",
    "\n",
    "d = np.linspace(1,10,5) #将1到10平均分成5段\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 4 9]\n",
      "[ True False False]\n",
      "[[1 4]\n",
      " [9 4]]\n",
      "[[ 7  4]\n",
      " [15 10]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3])\n",
    "a = a**2\n",
    "print(a)\n",
    "print(a<2) #返回布尔列表\n",
    "\n",
    "A = np.array([\n",
    "    [1,2],\n",
    "    [3,4]\n",
    "])\n",
    "B = np.array([\n",
    "    [1,2],\n",
    "    [3,1]\n",
    "])\n",
    "print(A*B) #对应元素相乘\n",
    "print(A@B) #矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.33035562 0.15008549 0.79166028 0.14788568]\n",
      " [0.84830674 0.81155255 0.48197765 0.20413976]\n",
      " [0.58414309 0.36544121 0.73088435 0.8486919 ]]\n",
      "6.295124325144497\n",
      "0.8486919048831624\n",
      "0.14788568215178954\n",
      "[1.41998707 2.3459767  2.52916055]\n",
      "[0.84830674 0.81155255 0.79166028 0.8486919 ]\n",
      "[0.14788568 0.20413976 0.36544121]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.random.random((3,4))# 随机生成3行4列的0-1的矩阵\n",
    "\n",
    "print(a)\n",
    "print(np.sum(a)) #结果是一个数字，将所有元素求和\n",
    "print(np.max(a)) #整个矩阵的最大值\n",
    "print(np.min(a)) #整个矩阵的最小值\n",
    "\n",
    "## 0 列   ，1行\n",
    "print(np.sum(a,axis = 1)) #在每一行求和 \n",
    "print(np.max(a,axis = 1)) #每一行的最大值\n",
    "print(np.min(a,axis = 1)) #每一行的最小值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]]\n",
      "11\n",
      "0\n",
      "7.5\n",
      "7.5\n",
      "[ 2  5  9 14 20 27 35 44 54 65 77 90]\n",
      "[[ 2  6 10]\n",
      " [ 3  7 11]\n",
      " [ 4  8 12]\n",
      " [ 5  9 13]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.arange(2,14).reshape((3,4))\n",
    "print(A)\n",
    "print(np.argmax(A))#在整个矩阵中找最大值[起始是0]\n",
    "print(np.argmin(A))#在整个矩阵中找最小值\n",
    "\n",
    "print(A.mean())\n",
    "print(np.mean(A))#求平均值，指定axis=0/1 指定列/行 计算\n",
    "print(A.cumsum())#累加\n",
    "\n",
    "print(A.T) #矩阵转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9]\n",
      "3\n",
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "[ 6  7  8  9 10]\n",
      "9\n",
      "9\n",
      "[3 8]\n",
      "[ 1  2  3  4  5  6  7  8  9 10]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.arange(1,10)\n",
    "print(A)\n",
    "print(A[2]) #索引用中括号\n",
    "\n",
    "B = np.arange(1,11).reshape((2,5))\n",
    "print(B)\n",
    "print(B[1])#取出第二列\n",
    "print(B[1][3])#第2，4个元素\n",
    "print(B[1,3])#第2，4个元素\n",
    "print(B[:,2])#第3列的所有数\n",
    "\n",
    "print(B.flatten()) #压缩成1维向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [3 2 4]]\n",
      "[1 2 3 3 2 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.array([1,2,3])\n",
    "B = np.array([3,2,4])\n",
    "C = np.vstack((A,B)) # 上下拼接\n",
    "D = np.hstack((A,B)) # 左右拼接\n",
    "print(C)\n",
    "print(D)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[111   2   3]\n",
      "[111   2   3]\n",
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.array([1,2,3])\n",
    "C = A.copy() # 不关联A和C\n",
    "B = A # 会关联A和B\n",
    "print(B)\n",
    "\n",
    "A[0]=111 # B会和A同步改变，改变 B 也会改变 A\n",
    "print(A)\n",
    "print(B)\n",
    "print(C)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "4ce0e62306dd6a5716965d4519ada776f947e6dfc145b604b11307c10277ef29"
  },
  "kernelspec": {
   "display_name": "Python 3.9.6 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
